<template>
  <div>
    <el-row>
      <el-button size="mini" type="primary" plain @click="onAddReport">
        新增
      </el-button>
      <el-button size="mini" type="primary" plain @click="openData">
        刷新
      </el-button>
    </el-row>
    <div style="height: 15px"></div>
    <el-dialog title="报表编辑" v-model="FShowReport" destroy-on-close>
      <el-form :model="FIndexReport" label-width="100px">
        <el-form-item label="报表编码">
          <el-input v-model="FIndexReport.FReportCode"></el-input>
        </el-form-item>
        <el-form-item label="报表名称">
          <el-input v-model="FIndexReport.FReportName"></el-input>
        </el-form-item>
        <el-form-item label="数据模板">
          <el-input v-model="FIndexReport.FModuleCode"></el-input>
        </el-form-item>
        <el-form-item label="报表源路径">
          <el-input
            v-model="FIndexReport.FFilePath"
            readonly
            placeholder="通过报表上传获得的路径"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="FShowReport = false">取 消</el-button>
          <el-button type="primary" @click="saveReport">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <el-table :data="FReportList" border>
      <el-table-column prop="FReportCode" label="报表代码" />
      <el-table-column prop="FReportName" label="报表名称" />
      <el-table-column prop="FFilePath" label="报表源路径" />
      <el-table-column prop="FModuleCode" label="数据模板" />
      <el-table-column prop="FReportRemark" label="报表备注" />
      <el-table-column label="操作" fixed="right" width="450px">
        <template #default="scope">
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            @click="onEditReport(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            @click="onUploadFile(scope.row)"
            >上传报表</el-button
          >
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            @click="onDownReport(scope.row)"
            >下载报表</el-button
          >
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            @click="onPrintReport(scope.row)"
            >报表打印</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script lang="ts">
import ReportAdminApi, {
  TBaseReport,
} from "@/baselib/module/report/ReportAdminApi";
import FileDialogApi from "@/baselib/aPublic/FileDialog";
import TFormBase from "@/views/BaseForm";
import Compressor from "compressorjs";
import UploadFileApi from "@/baselib/aPublic/UploadFileApi";
import PublicApi from "@/baselib/aPublic/PublicApi";
import {
  TFastApiRequest,
  TReportPostData,
} from "@/baselib/aPublic/PublicClass";
export default class TFormReportList extends TFormBase {
  private FShowReport: boolean = false;
  private FIndexReport: TBaseReport = new TBaseReport();
  private FReportList: TBaseReport[] = [];
  public created() {
    this.openData();
  }
  private async openData() {
    var lResult = await ReportAdminApi.GetBaseReportList();
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.FReportList = lResult.resultData;
    this.$myMsgHelp.msgHintAutoClose("刷新数据成功");
  }
  private onAddReport() {
    this.FIndexReport = new TBaseReport();
    this.FShowReport = true;
  }
  private onEditReport(qCms: TBaseReport) {
    //
    this.FIndexReport = qCms;
    this.FShowReport = true;
  }
  private async saveReport() {
    this.FShowReport = false;
    var lResult = await ReportAdminApi.SaveBaseReport(this.FIndexReport);
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.$myMsgHelp.msgHintAutoClose("保存数据成功");
    this.openData();
  }
  private async onUploadFile(qItem: TBaseReport) {
    let lFiles = await FileDialogApi.OpenFileDialog();
    //上传文件
    let lFile = lFiles[0];
    //压缩上传
    let lFormData: FormData = new FormData();
    lFormData.append("reportID", qItem.FReportID);
    lFormData.append("file", lFile);
    let lResult = await ReportAdminApi.UploadReportAsync(lFormData);
    //
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.$myMsgHelp.msgHintAutoClose("上传文件成功");
    this.openData();
  }
  private async onDownReport(qItem: TBaseReport) {
    let lResult = await ReportAdminApi.DownReportFile(qItem.FReportID);
    //
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog("下载文件失败");
      return;
    }
  }
  private async onPrintReport(qItem: TBaseReport) {
    var lReportPostData: TReportPostData = new TReportPostData();
    lReportPostData.reportCode = qItem.FReportCode;
    let lResult = await PublicApi.FastReport(lReportPostData);
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
  }
}
</script>

